package com.xbongbong.paas.statistics.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasAggRuleEntity;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.statistics.model.AggRuleModel;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleAddDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleDelDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleGetDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleListDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleUpdateDTO;
import com.xbongbong.paas.statistics.pojo.vo.AggRuleGetVO;
import com.xbongbong.paas.statistics.pojo.vo.AggRuleListVO;
import com.xbongbong.paas.statistics.pojo.vo.AggRuleSaveVO;
import com.xbongbong.paas.statistics.service.AggRuleService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建时间： 2018/9/18 10:52
 * 修改时间： 2018/9/18 10:52 梁鲁江
 *
 * @author 梁鲁江
 */
@Service("aggRuleService")
public class AggRuleServiceImpl implements AggRuleService {

    private static final Logger LOG = LoggerFactory.getLogger(AggRuleServiceImpl.class);
    @Resource
    private AggRuleModel aggRuleModel;
    @Resource
    private MongoLogHelp mongoLogHelp;
    @Resource
    private UserModel userModel;
    @Override
    public AggRuleSaveVO addPaasAggRule(AggRuleAddDTO aggRuleAddDTO) throws XbbException {
        PaasAggRuleEntity entity = new PaasAggRuleEntity();
        BeanUtil.copyProperties(aggRuleAddDTO,entity);
        entity.setCreatorId(aggRuleAddDTO.getUserId());
        entity.setColTitle(JSONArray.toJSONString(aggRuleAddDTO.getColTitle()));
        entity.setRowTitle(JSONArray.toJSONString(aggRuleAddDTO.getRowTitle()));
        entity.setSumAttr(JSONArray.toJSONString(aggRuleAddDTO.getSumAttr()));
        entity.setFilterAttr(JSONArray.toJSONString(aggRuleAddDTO.getFilterAttr()));
        entity.setRelateAttr(JSONArray.toJSONString(aggRuleAddDTO.getRelateAttr()));
        entity.setPosition(JSONObject.toJSONString(aggRuleAddDTO.getPosition()));
        AggRuleSaveVO aggRuleSaveVO = new AggRuleSaveVO();
        try {
            aggRuleModel.insert(entity);
            aggRuleSaveVO.setId(entity.getId());

        } catch (Exception e) {
            LOG.error("统计规则保存出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        String corpid = aggRuleAddDTO.getCorpid();
        String userId = aggRuleAddDTO.getUserId();

        // 记录日志
        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.AGG_MEMO_ADD_RULE), aggRuleAddDTO.getLoginUserName(),entity.getName());
        mongoLogHelp.buildLog(corpid, userId, aggRuleAddDTO.getLoginUserName(), OperateModuleTypeEnum.PAAS_STATISTICS, OperateTypeEnum.NEW, entity.getId().toString(), entity.getName(), memo, aggRuleAddDTO.getHttpHeader());
        return aggRuleSaveVO;
    }

    @Override
    public AggRuleSaveVO updatePaasAggRule(AggRuleUpdateDTO aggRuleUpdateDTO) throws XbbException {
        PaasAggRuleEntity entity = new PaasAggRuleEntity();
        BeanUtil.copyProperties(aggRuleUpdateDTO,entity);
        AggRuleSaveVO aggRuleSaveVO = new AggRuleSaveVO();
        try {
            entity.setColTitle(JSONArray.toJSONString(aggRuleUpdateDTO.getColTitle()));
            entity.setRowTitle(JSONArray.toJSONString(aggRuleUpdateDTO.getRowTitle()));
            entity.setSumAttr(JSONArray.toJSONString(aggRuleUpdateDTO.getSumAttr()));
            entity.setFilterAttr(JSONArray.toJSONString(aggRuleUpdateDTO.getFilterAttr()));
            entity.setRelateAttr(JSONArray.toJSONString(aggRuleUpdateDTO.getRelateAttr()));
            entity.setPosition(JSONObject.toJSONString(aggRuleUpdateDTO.getPosition()));
            aggRuleModel.update(entity);
            aggRuleSaveVO.setId(entity.getId());
        } catch (Exception e) {
            LOG.error("统计规则更新出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        String corpid = aggRuleUpdateDTO.getCorpid();
        String userId = aggRuleUpdateDTO.getUserId();
        String userName = aggRuleUpdateDTO.getLoginUserName();

        // 记录日志
        String logMemoUpdateTemplate = I18nMessageUtil.getMessage(LogMemoConstant.AGG_MEMO_UPDATE_RULE);
        OperateTypeEnum operateTypeEnum = OperateTypeEnum.EDIT;
        String memo = String.format(logMemoUpdateTemplate, userName, entity.getName());
        mongoLogHelp.buildLog(corpid, userId, userName, OperateModuleTypeEnum.PAAS_STATISTICS, operateTypeEnum, entity.getId().toString(), entity.getName(), memo, aggRuleUpdateDTO.getHttpHeader());
        return aggRuleSaveVO;
    }

    @Override
    public AggRuleSaveVO delPaasAggRule(AggRuleDelDTO aggRuleDelDTO) throws XbbException {
        AggRuleSaveVO aggRuleSaveVO = new AggRuleSaveVO();
        try {
            aggRuleModel.delete(aggRuleDelDTO.getId(),aggRuleDelDTO.getCorpid());
            aggRuleSaveVO.setId(aggRuleDelDTO.getId());
        } catch (Exception e) {
            LOG.error("统计规则删除出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        String corpid = aggRuleDelDTO.getCorpid();
        String userId = aggRuleDelDTO.getUserId();
        String userName = aggRuleDelDTO.getLoginUserName();
        // 记录日志
        String logMemoUpdateTemplate = I18nMessageUtil.getMessage(LogMemoConstant.AGG_MEMO_DELETE_RULE);
        OperateTypeEnum operateTypeEnum = OperateTypeEnum.DELETE;
        String memo = String.format(logMemoUpdateTemplate, userName ,aggRuleDelDTO.getName());
        mongoLogHelp.buildLog(corpid, userId, userName, OperateModuleTypeEnum.PAAS_STATISTICS, operateTypeEnum, aggRuleDelDTO.getId().toString(), aggRuleDelDTO.getName(), memo, aggRuleDelDTO.getHttpHeader());
        return aggRuleSaveVO;
    }

    @Override
    public AggRuleGetVO getPaasAggRule(AggRuleGetDTO aggRuleGetDTO) throws XbbException {
        PaasAggRuleEntity entity = aggRuleModel.getByKey(aggRuleGetDTO.getId(), aggRuleGetDTO.getCorpid());
        AggRuleGetVO aggRuleGetVO = new AggRuleGetVO();
        BeanUtil.copyProperties(entity,aggRuleGetVO);
        return aggRuleGetVO;
    }

    @Override
    public AggRuleListVO listPaasAggRule(AggRuleListDTO aggRuleListDTO) throws XbbException {
        AggRuleListVO aggRuleListVO = new AggRuleListVO();
        try {
            List<AggRuleGetVO> aggRulelist = new ArrayList<>();
            Map<String, Object> params = BeanUtil.convertBean2Map(aggRuleListDTO);
            List<PaasAggRuleEntity> list = aggRuleModel.list(params);
            BeanUtil.copyPropertiesList(list,aggRulelist,AggRuleGetVO.class);
            aggRuleListVO.setAggRulelist(aggRulelist);
        } catch (Exception e) {
            LOG.error("统计规则列表查询出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return aggRuleListVO;
    }


}
